﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetfractionHub.Network.Protocol;
using NetfractionHub.Network.Protocol.BASE.Factory;
using Netfraction.Common.Logging;

namespace NetfractionHub.LocalMachine.BotCommands.Handler
{
    [CommandHandler("kick")]
    public class Kick : CommandHandler
    {
        public override BotReturnStatus HandleCommandMessage(NetfractionHub.RemoteMachine.Node source, CommandMessage message)
        {
            if (source == null)
                return new BotReturnStatus(false);

            string userToKick = string.Empty;
            string reason = string.Empty;
            if (message.StringArguments.Count == 1)
                userToKick = message.StringArguments.ContainsKey("user") ? message.StringArguments["user"] : string.Empty;
            else if (message.StringArguments.Count == 2)
            {
                userToKick = message.StringArguments.ContainsKey("user") ? message.StringArguments["user"] : string.Empty;
                reason = message.StringArguments.ContainsKey("reason") ? message.StringArguments["reason"] : string.Empty;
            }
            else if (message.UnnamedArguments.Count == 1)
                userToKick = message.UnnamedArguments[0];
            else if (message.UnnamedArguments.Count == 2)
            {
                userToKick = message.UnnamedArguments[0];
                reason = message.UnnamedArguments[1];
            }
            else
                return new BotReturnStatus(false, "Missing string parameter 'user'.");  // should reply with the help string for the function

            if (string.IsNullOrEmpty(userToKick))
                return new BotReturnStatus(false, "Missing string parameter 'user'.");
              
            if (!Security.Permission.HasPermission(source.SecurityIdentity, typeof(Security.Permissions.KickIdentityPermission)))
                return new BotReturnStatus(false, Security.Permission.GetDeniedString(typeof(Security.Permissions.KickIdentityPermission)));

            RemoteMachine.Node nodeToKick = Network.ConnectionManager.GetConnectedNodeByNickname(userToKick);
            if (nodeToKick != null)
            {
                if (!Security.RoleManager.IsInAncestorTree(source.SecurityIdentity.Role, nodeToKick.SecurityIdentity.Role))
                    return new BotReturnStatus(false, string.Format("Permission denied. You are not an ancestor of user '{0}'.", userToKick));

                string messageToRespond = string.Empty;
                if (string.IsNullOrEmpty(reason))
                    messageToRespond = string.Format("Kicking '{0}'.", userToKick);
                else
                    messageToRespond = string.Format("Kicking '{0}': {1}.", userToKick, reason);

                if (message.BooleanArguments.ContainsKey("public") && message.BooleanArguments["public"] == true)
                    this.ParentBot.SendBroadcastMessageFromBot(messageToRespond);
                else
                    this.ParentBot.SendEchoMessageFromBot(source, messageToRespond);

                LogManager.WriteEntry(messageToRespond, LogEventId.KickIdentity, LogLevel.Detailed);

                // should be sending a STA here...??? STA 32
                //nodeToKick.Connection.Disconnect();
                Network.ConnectionManager.KickNode(source, nodeToKick, reason, 90);
            }
            else
                return new BotReturnStatus(false, string.Format("Cannot kick, the user '{0}' does not exist.", userToKick));
            
            return new BotReturnStatus(false);
        }
    }
}
